package com.example.admin.service.impl;

import com.example.admin.model.LoginLog;
import com.example.admin.model.OperationLog;
import com.example.admin.repository.LoginLogRepository;
import com.example.admin.repository.OperationLogRepository;
import com.example.admin.service.LogService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class LogServiceImpl implements LogService {
    private final OperationLogRepository operationLogRepository;
    private final LoginLogRepository loginLogRepository;

    public LogServiceImpl(OperationLogRepository operationLogRepository, 
                         LoginLogRepository loginLogRepository) {
        this.operationLogRepository = operationLogRepository;
        this.loginLogRepository = loginLogRepository;
    }

    @Override
    public Page<OperationLog> getOperationLogs(Long userId, Pageable pageable) {
        if (userId != null) {
            return operationLogRepository.findByUserId(userId, pageable);
        }
        return operationLogRepository.findAll(pageable);
    }

    @Override
    public Page<LoginLog> getLoginLogs(Long userId, Pageable pageable) {
        if (userId != null) {
            return loginLogRepository.findByUserId(userId, pageable);
        }
        return loginLogRepository.findAll(pageable);
    }

    @Override
    public void saveOperationLog(OperationLog log) {
        operationLogRepository.save(log);
    }

    @Override
    public void saveLoginLog(LoginLog log) {
        loginLogRepository.save(log);
    }
}
